/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shop.viewer;

//import demo.EqcUsers;
import idea.model.qnrlibrary.EqcUsers;
import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;
import org.openide.windows.WindowManager;

/**
 *
 * @author poiss002
 */
public class EqcUsersChildFactory extends ChildFactory<EqcUsers> {

    private List<EqcUsers> resultList;

    public EqcUsersChildFactory(List<EqcUsers> resultList) {
        this.resultList = resultList;
    }

    @Override
    protected boolean createKeys(List<EqcUsers> list) {
        for (EqcUsers EqcUsers : resultList) {
            list.add(EqcUsers);
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(EqcUsers c) {
        try {
            return new EqcUsersBeanNode(c);
        } catch (IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
            return null;
        }
    }

    private class EqcUsersBeanNode extends BeanNode {

        public EqcUsersBeanNode(EqcUsers bean) throws IntrospectionException {
            super(bean, Children.LEAF, Lookups.singleton(bean));
        }

        @Override
        public Action[] getActions(boolean bln) {
            //Action[] oldActions = super.getActions (bln);
            //Action[] newActions = new Action[ oldActions.length + 2];
            Action[] newActions = new Action[2];
            //System.arraycopy(oldActions, 0 , newActions, 0, oldActions.length);
            //newActions[oldActions.length ] = new AddUserAction();
            newActions[0] = new AddUserAction();
            //newActions[oldActions.length + 1] = new DeleteUserAction();
            newActions[1] = new DeleteUserAction();
            return newActions;
        }

        @Override
        public Action getPreferredAction() {
            Action a = super.getPreferredAction();
            return null;
        }

        private class DrillDownAction extends AbstractAction {

            public DrillDownAction() {
                putValue(NAME, "Drill Down");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                drillDown();
            }
        }

        private class AddUserAction extends AbstractAction {

            public AddUserAction() {
                putValue(NAME, "Add New User");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                addnew();
            }
        }

        private class DeleteUserAction extends AbstractAction {

            public DeleteUserAction() {
                putValue(NAME, "Delete This User");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                deleteThis();
            }
        }

        private void addnew() {
            UserViewerTopComponent uvtc = (UserViewerTopComponent) WindowManager.getDefault().findTopComponent("UserViewerTopComponent");
            uvtc.refreshNode();
            System.out.println("add new record");
        }

        private void deleteThis() {
            System.out.println("delete this record");
        }

        private void drillDown() {
            System.out.println("Drill down ");
        }
    }
}